﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting;
using System.Text;
using Imgur.Net.JSON;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using NUnit.Framework;

namespace Imgur.Net.Test.JSON
{
    [TestFixture]
    public class EpochDateTimeConverterTests
    {
        [Test]
        public void DateTImeValueWriteJsonTest()
        {
            var instance = new EpochDateTimeConverter();
            using (StringWriter sw = new StringWriter())
            {
                using (JsonWriter writer = new JsonTextWriter( sw ) )
                {
                    JsonSerializer serializer = new JsonSerializer();
                    DateTime date = new DateTime(1970, 1, 1, 1, 0, 0, DateTimeKind.Utc);

                    // epoch should be 60s * 60m after the Jan 1 epoch

                    instance.WriteJson(writer, date, serializer);
                    writer.Flush();

                    long epochTime = Convert.ToInt64(sw.ToString());

                    Assert.AreEqual(60*60, epochTime);
                }
            }
        }

        [Test]
        public void DateTimeOffsetValueWriteJsonTest()
        {
            var instance = new EpochDateTimeConverter();
            using (StringWriter sw = new StringWriter())
            {
                using (JsonWriter writer = new JsonTextWriter(sw))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    DateTimeOffset date = new DateTimeOffset(new DateTime(1970, 1, 1, 1, 0, 0, DateTimeKind.Utc),
                        TimeSpan.Zero);

                    // epoch should be 60s * 60m after the Jan 1 epoch

                    instance.WriteJson(writer, date, serializer);
                    writer.Flush();

                    long epochTime = Convert.ToInt64(sw.ToString());

                    Assert.AreEqual(60 * 60, epochTime);
                }
            }
        }

        [Test]
        [ExpectedException(typeof(JsonSerializationException))]
        public void OtherValueTypeWriteJsonTest()
        {
            var instance = new EpochDateTimeConverter();
            using (StringWriter sw = new StringWriter())
            {
                using (JsonWriter writer = new JsonTextWriter(sw))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    long date = 12345L;

                    instance.WriteJson(writer, date, serializer);
                }
            }
        }

        [Test]
        public void StandardIntegerReadJsonTest()
        {
            var instance = new EpochDateTimeConverter();
            string json = "3600";
            using (StringReader sr = new StringReader(json))
            {
                using (JsonReader reader = new JsonTextReader(sr ) )
                {
                    reader.Read();
                    JsonSerializer serializer = new JsonSerializer();

                    DateTime d = (DateTime) instance.ReadJson(reader, typeof (DateTime), null, serializer);
                    
                    DateTime expectedDate = new DateTime(1970, 1, 1, 1, 0, 0, DateTimeKind.Utc);
                    Assert.AreEqual(expectedDate, d);
                }
            }
        }

        [Test]
        [ExpectedException(typeof(JsonSerializationException))]
        public void NonIntegerReadJsonTest()
        {
            var instance = new EpochDateTimeConverter();
            string json = "\"Non Integer\"";
            using (StringReader sr = new StringReader(json))
            {
                using (JsonReader reader = new JsonTextReader(sr))
                {
                    reader.Read();
                    JsonSerializer serializer = new JsonSerializer();

                    DateTime d = (DateTime)instance.ReadJson(reader, typeof(DateTime), null, serializer);

                    DateTime expectedDate = new DateTime(1970, 1, 1, 1, 0, 0, DateTimeKind.Utc);
                    Assert.AreEqual(expectedDate, d);
                }
            }
        }
    }
}
